source is null for getProperty(null, “name”)

这个问题的发生一般是在 Mapper.xml 的 sql语句中出错。

我在项目中使用到了,用到了XML中的判断条件查询方式,代码如下:

<if test="employee.name != null and employee.name != ''">
AND e.`name` LIKE CONCAT( '%', #{employee.name}, '%' )
</if>

上述语句的简单来说,当传过来的employee.name不等于 null 并且不等于 '' ,就按这个条件查询,这时候运行项目,报错:

==source is null for getProperty(null, “name”)==

这时候可能有两个原因:

一、你并没有对应的对象参数

你可以查看方法参数中是否有对应的对象参数

例如:我这个方法中要查看是否有 employee

//  错误
List<Employee> getEmployee();

// 正确
List<Employee> getEmployee(Employee employee);

二、有对应对象,但是对象传值为NULL

这种情况可以在外层加入判断:

<if test="employee != null and employee != '' ">
<if test="employee.name != null and employee.name != ''">
AND e.`name` LIKE CONCAT( '%', #{employee.name}, '%' )
</if>
</if>

这样,没有传入任何参数时,也不会报错了